package collections;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

public class ArrayListTest {
	public static void printElement(Collection c) {
		Iterator it = c.iterator();
		while(it.hasNext())
		{
			System.out.println(it.next());
		}
	}

	public static void main(String[] args) {
		/*
		ArrayList al = new ArrayList();
		al.add("qqlin");//add()是根集合类collection中定义的
		al.add("qqmage");
		al.add("qqlink");
		al.add(new Point(1,2));
		al.add(new Point(2,4));
		
		for(int i=0;i<al.size();i++)
		{
			System.out.println(al.get(i));
		}
		System.out.println(al);

		Object[] objs = al.toArray();
		for(int i=0;i<objs.length;i++)
		{
			System.out.println(objs[i]);
		}
		
		List l = Arrays.asList(objs);
		//l.add("zhangsan");
		System.out.println(l);
		
		Object subedOb = l.set(1, "cqqlin");
		System.out.println(l);
		System.out.println(subedOb);
		
		Iterator it = al.iterator();
		//it = Arrays.asList(objs).iterator();
		it.next();
		it.remove();
		while(it.hasNext())
		{
			System.out.println(it.next());
		}
		
		printElement(al);
		printElement(l);
		*/
		
		ArrayList a1 = new ArrayList();
		a1.add(new Student(2,"zhangsan"));
		a1.add(new Student(1,"lisi"));
		a1.add(new Student(3,"wangwu"));
		a1.add(new Student(3,"linke"));
		Collections.sort(a1,new Student.StudentComparator());
		printElement(a1);
		Collections.sort(a1,Collections.reverseOrder());
		printElement(a1);
				
	}
}

class Point {
	int x;
	int y;
	Point(int x,int y) {
		this.x = x;
		this.y = y;
	}
	
	public String toString() {
		return "x="+x+" y="+y;
	}
}

class Student implements Comparable
{
	int num;
	String name;
	static class StudentComparator implements java.util.Comparator {
		public int compare(Object o1, Object o2) {
			// TODO Auto-generated method stub
			Student s1 = (Student)o1;
			Student s2 = (Student)o2;
			int result = s1.num > s2.num? 1 : (s1.num == s2.num? (s1.name.compareTo(s2.name)) : -1);
			return result;
		}
	}
	
	
	Student(int num, String name)
	{
		this.num = num;
		this.name = name;
	}
	public int compareTo(Object o) {
		// TODO Auto-generated method stub
		Student s = (Student)o;
		return this.num>s.num ? 1 : (this.num==s.num? 0 : -1);
	}
	public String toString()
	{
		return num+":"+name;
	}
}